package ute.mvc.dao;


import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ute.mvc.model.Subject;
@Repository("subjectDAO")
public class SubjectDAOImpl implements SubjectDAO{
	@Autowired
	SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<Subject> getAllSubject() {
		return sessionFactory.getCurrentSession()
				.createCriteria(Subject.class)
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.list();
	}

	@SuppressWarnings("unchecked")
	public List<Subject> findSubjectByName(String subjectName) {
		return sessionFactory.getCurrentSession()
				.createCriteria(Subject.class)
				.add(Restrictions.like("", "%" + subjectName + "%"))
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.list();
	}

	public void addSubject(Subject subject) {
		sessionFactory.getCurrentSession().save(subject);
	}

	public void updateSubject(Subject subject) {
		sessionFactory.getCurrentSession().merge(subject);
	}

	public void deleteSubject(Subject subject) {
		sessionFactory.getCurrentSession().delete(subject);
	}

	public Subject getSubjectById(String id) {
		// TODO Auto-generated method stub
		return (Subject) sessionFactory.getCurrentSession().get(Subject.class, id);
	}

}
